package com.nbufe.controller;

import com.github.pagehelper.PageInfo;
import com.nbufe.pojo.BookInfo;
import com.nbufe.pojo.LendList;
import com.nbufe.pojo.Notice;
import com.nbufe.pojo.ReaderInfo;
import com.nbufe.service.BookInfoService;
import com.nbufe.service.LendListService;
import com.nbufe.service.NoticeService;
import com.nbufe.service.ReaderInfoService;
import com.nbufe.vo.LendBookVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class BaseController {

    @Resource
    private NoticeService noticeService;

    @Resource
    private BookInfoService bookInfoService;

    @Resource
    private LendListService lendListService;

    @Resource
    private ReaderInfoService readerInfoService;

    /**
     * 首页
     * @return
     */
    @GetMapping("/index")
    public String index(){
        return "index";
    }

    /**
     * 欢迎页面跳转
     * @return
     */
    @GetMapping("/welcome")
    public String welcome(Model model){
        // 提供公告信息
        PageInfo<Notice> pageInfo =  noticeService.queryAllNotice(null,1,5);
        if (pageInfo != null){
            List<Notice> noticeList = pageInfo.getList();
            model.addAttribute("noticeList",noticeList);
        }

        // 根据图书类型查询图书数量
        List<BookInfo> list = bookInfoService.getBookCountByType();
        model.addAttribute("list",list);

        // 借书时间线
        List<LendList> lendLists = lendListService.queryLookBookList(null, null);
        model.addAttribute("info", lendLists);

        // 个人借书情况
        // 先查找出所有读者的id
        List<Integer> readerIdList = readerInfoService.queryAllReaderId();
        List<LendBookVo> bookVoList = new ArrayList<>();
        for (Integer readerId : readerIdList) {
            LendBookVo lendBookVo = lendListService.queryLendBookVoListAll(readerId);
            ReaderInfo readerInfo = readerInfoService.queryReaderInfoById(lendBookVo.getReaderId());
            lendBookVo.setReaderName(readerInfo.getRealName());
            bookVoList.add(lendBookVo);
        }
        model.addAttribute("lendBooks", bookVoList);
        System.out.println(bookVoList);

        return "welcome";
    }

    @GetMapping("/updatePassword")
    public String updatePwd(){
        return "pwdUpdate/updatePwd";
    }

}
